﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Nhandang
{
    public partial class Form1 : Form
    {
        Network nw;
        string ImageBinary = "";
        string path = "";
        //string path2 = "";
        int bo = 0, k=9;
        string bsx;
        public Form1()
        {
            InitializeComponent();
        }
        OpenFileDialog op = new OpenFileDialog();
       

        private void button1_Click(object sender, EventArgs e)
        {
            op.Filter = "Image File (*.jpg,*.png)|*.jpg;*.png";
            if (op.ShowDialog() == DialogResult.OK)
            {
                path = "E:\\Nhandang\\Nhandang\\bin\\Debug\\20 lop 60\\";
                IplImage anh = cvlib.cvLoadImage(op.FileName);
                ImageBinary = MakeImageBinary.ImageBinary(op.FileName, "0");
                pictureBox1.Image = (Bitmap)anh;
                //path2 = path + bo + "wafasdf";
                //MessageBox.Show(path2);
                StreamReader reader = File.OpenText(path + "outputW.ann");
                int hiden = -1;
                try
                {
                    string[] string_ = reader.ReadLine().Split(',');
                    reader.Close();
                    foreach (string s in string_)
                    {
                        hiden = ++hiden;
                        //MessageBox.Show(hiden.ToString());
                    }
                }
                catch (Exception) { }
                // MessageBox.Show(hiden.ToString());
                nw = new Network(256, hiden);
                nw.Initialise(path + "hiddenW.ann", path + "outputW.ann");




            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            IplImage anh1 = cvlib.cvLoadImage("C:\\Users\\akazu\\Documents\\Visual Studio 2010\\Projects\\demoopen\\demoopen\\index.jpg");
            pictureBox1.Image = (Bitmap)anh1;//load anh goc ra picture box
            string pathbs;//
            path = "D:\\- MyWorks -\\Ky 9\\DA TOT NGHIEP\\New folder (4)\\Nhandang\\Nhandang\\bin\\Debug";
            //cat hinh va lan luot dua vao xu ly\
            // neu nhu trong khi < k thi tiep tuc
            while(bo<k)
            {
                int kq=0;
                double kqpt=0;

                pathbs = "C:\\Users\\akazu\\Documents\\Visual Studio 2010\\Projects\\demoopen\\demoopen\\chuso"+bo+".jpg";
                //mvoermoemob
                ImageBinary = MakeImageBinary.ImageBinary(pathbs, "0");

                int i = 0;
               // while (i < 31)
                while (i < 10)
                {
                    // MessageBox.Show(path);
                    i = i + 1;

                    StreamReader reader = File.OpenText(path+i+"\\" + "outputW.ann");
                    int hiden = -1;
                    try
                    {
                        string[] string_ = reader.ReadLine().Split(',');
                        reader.Close();
                        foreach (string s in string_)
                        {
                            hiden = ++hiden;
                            //MessageBox.Show(hiden.ToString());
                        }
                    }
                    catch (Exception) { }
                    // MessageBox.Show(hiden.ToString());
                    nw = new Network(256, hiden);
                    nw.Initialise(path + i + "\\" + "hiddenW.ann", path + i + "\\" + "outputW.ann");
                 //   nw.Initialise(path + "hiddenW.ann", path + "outputW.ann");
                }


                if (path == "" || ImageBinary == "")
                { MessageBox.Show("Chưa chọn ảnh nhận dạng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); }
                else
                {
                    double value = nw.Test(ImageBinary);
                    textBox1.Text = value.ToString();
                    if(value>kqpt){           //lay kq phan tich co do chinh xac cao nhat :)
                        kq=i;
                        kqpt=value;
                    }
                    //if (value >= 0.5)
                    //{
                    //    MessageBox.Show("đúng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //}
                    //else
                    //    MessageBox.Show("sai", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                //}
                switch(kq){
                    case 1: bsx = bsx + "0"; break;
                    case 2: bsx = bsx + "1"; break;
                    case 3: bsx = bsx + "2"; break;
                    case 4: bsx = bsx + "3"; break;
                    case 5: bsx = bsx + "4"; break;
                    case 6: bsx = bsx + "5"; break;
                    case 7: bsx = bsx + "6"; break;
                    case 8: bsx = bsx + "7"; break;
                    case 9: bsx = bsx + "8"; break;
                    case 10: bsx = bsx + "9"; break;
                    case 11: bsx = bsx + "a"; break;
                    case 12: bsx = bsx + "b"; break;
                    case 13: bsx = bsx + "c"; break;
                    case 14: bsx = bsx + "d"; break;
                    case 15: bsx = bsx + "e"; break;
                    case 16: bsx = bsx + "f"; break;
                    case 17: bsx = bsx + "g"; break;
                    case 18: bsx = bsx + "h"; break;
                    case 19: bsx = bsx + "k"; break;
                    case 20: bsx = bsx + "l"; break;
                    case 21: bsx = bsx + "m"; break;
                    case 22: bsx = bsx + "n"; break;
                    case 23: bsx = bsx + "p"; break;
                    case 24: bsx = bsx + "r"; break;
                    case 25: bsx = bsx + "s"; break;
                    case 26: bsx = bsx + "t"; break;
                    case 27: bsx = bsx + "u"; break;
                    case 28: bsx = bsx + "v"; break;
                    case 29: bsx = bsx + "x"; break;
                    case 30: bsx = bsx + "y"; break;
                    case 31: bsx = bsx + "z"; break;

                }
                bo++;
            }
            //xuat ra
            MessageBox.Show(bsx, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
    }
}
